﻿//QuikSettings.xaml.cs
//Copyright (c) 2013 StockSharp LLC, all rights reserved.
//This code module is part of StockSharp library.
//This code is licensed under the GNU GENERAL PUBLIC LICENSE Version 3.
//See the file License.txt for the license details.
//More info on: http://stocksharp.com

namespace StockSharp.Quik.Xaml
{
	using System.Windows;

	using Ecng.Collections;
	using Ecng.Common;
	using Ecng.Xaml;

	using Ookii.Dialogs.Wpf;

	using StockSharp.BusinessEntities;
	using StockSharp.Quik;
	using StockSharp.Xaml;

	/// <summary>
	/// Визуальная панель настроек <see cref="QuikTrader"/>.
	/// </summary>
	public partial class QuikSettings : ITraderSettingsPanel
	{
		/// <summary>
		/// Создать <see cref="QuikSettings"/>.
		/// </summary>
		public QuikSettings()
		{
			InitializeComponent();
			Path = QuikTerminal.GetDefaultPath();
		}

		/// <summary>
		/// <see cref="QuikTrader.Path"/>.
		/// </summary>
		public string Path
		{
			get { return PathCtrl.Text; }
			set { PathCtrl.Text = value; }
		}

		private void FindPath_Click(object sender, RoutedEventArgs e)
		{
			var dlg = new VistaFolderBrowserDialog();

			if (!PathCtrl.Text.IsEmpty())
				dlg.SelectedPath = PathCtrl.Text;

			if (dlg.ShowDialog(this.GetWindow()) == true)
			{
				PathCtrl.Text = dlg.SelectedPath;
			}
		}

		bool ITraderSettingsPanel.IsValid
		{
			get { return !Path.IsEmpty(); }
		}

		void ITraderSettingsPanel.Save(ITrader trader)
		{
			var quikTrader = (QuikTrader)trader;

			quikTrader.Path = Path;
			quikTrader.SecuritiesTable.Columns.RemoveWhere(c => !c.IsMandatory);
			quikTrader.SecuritiesTable.Columns.AddRange(SecurityColumns.GetSelectedColumns());
		}

		void ITraderSettingsPanel.Load(ITrader trader)
		{
			var quikTrader = (QuikTrader)trader;

			Path = quikTrader.Terminal != null ? quikTrader.Terminal.FileName : QuikTerminal.GetDefaultPath();

			SecurityColumns.SelectColumns(quikTrader.SecuritiesTable.Columns);
		}
	}
}